home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / extapi.js < prev    next >
Text File  |  2008-01-24  |  4KB  |  169 lines

  1. /*
  2.     extapi.js
  3.  
  4.     Copyright ┬⌐ 2008  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. function wot_extapi()
  8. {
  9. }
  10.  
  11. wot_extapi.prototype =
  12. {
  13.     /* Public constants */
  14.     STATUS_EXCELLENT:        2,
  15.     STATUS_GOOD:            1,
  16.     STATUS_UNTRUSTED:        0,
  17.     STATUS_UNAVAILABLE:        -1,
  18.     STATUS_AGAIN:            -2,
  19.  
  20.     /* Internal constants */
  21.     __STATUS_INPROGRESS:    -3,
  22.     __POLL_TIMEOUT:            500,
  23.  
  24.     /* Internal methods */
  25.     __istrusted: function(target, cs)
  26.     {
  27.         try {
  28.             var rv = this.STATUS_EXCELLENT;
  29.  
  30.             for (var i = 0; i < 3; ++i) {
  31.                 if (cs == WOT_QUERY_OK) {
  32.                     var t = wot_cache.get(target, "testimony_"  + i);
  33.  
  34.                     if (t >= 0) {
  35.                         if (t < WOT_MIN_REPUTATION_4) {
  36.                             return this.STATUS_UNTRUSTED;
  37.                         } else if (t < WOT_MIN_REPUTATION_5) {
  38.                             rv = this.STATUS_GOOD;
  39.                         }
  40.                     }
  41.                 }
  42.  
  43.                 var r = wot_cache.get(target, "reputation_" + i);
  44.                 var c = wot_cache.get(target, "confidence_" + i);
  45.  
  46.                 if (r < WOT_MIN_REPUTATION_4 || c < WOT_MIN_CONFIDENCE_1) {
  47.                     return this.STATUS_UNTRUSTED;
  48.                 } else if (r < WOT_MIN_REPUTATION_5) {
  49.                     rv = this.STATUS_GOOD;
  50.                 }
  51.             }
  52.  
  53.             return rv;
  54.         } catch (e) {
  55.             dump("wot_extapi.__istrusted: failed with " + e + "\n");
  56.         }
  57.  
  58.         return this.STATUS_UNAVAILABLE;
  59.     },
  60.  
  61.     __getcachedstatus: function(target)
  62.     {
  63.         try {
  64.             if (wot_cache.iscached(target)) {
  65.                 if (wot_cache.get(target, "inprogress")) {
  66.                     return this.__STATUS_INPROGRESS;
  67.                 }
  68.  
  69.                 var cs = wot_cache.get(target, "status");
  70.  
  71.                 if (cs == WOT_QUERY_OK || cs == WOT_QUERY_LINK) {
  72.                     return this.__istrusted(target, cs);
  73.                 } else if (cs == WOT_QUERY_ERROR &&
  74.                         (Date.now() - wot_cache.get(target, "time")) <
  75.                             WOT_INTERVAL_CACHE_REFRESH_ERROR) {
  76.                     return this.STATUS_UNAVAILABLE;
  77.                 }
  78.             }
  79.  
  80.             return this.STATUS_AGAIN;
  81.         } catch (e) {
  82.             dump("wot_extapi.__getcachedstatus: failed with " + e + "\n");
  83.         }
  84.  
  85.         return this.STATUS_UNAVAILABLE;
  86.     },
  87.  
  88.     __pollstatus: function(target, callback)
  89.     {
  90.         try {
  91.             /* Ratings are being fetched, poll the cache until the
  92.                  request completes */
  93.             if (typeof(callback) == "function") {
  94.                 window.setTimeout(function(instance) {
  95.                         instance.getstatus(target, callback);
  96.                     }, this.__POLL_TIMEOUT, this);
  97.             }
  98.  
  99.             return this.STATUS_AGAIN;
  100.         } catch (e) {
  101.             dump("wot_extapi.__pollstatus: failed with " + e + "\n");
  102.         }
  103.  
  104.         return this.STATUS_UNAVAILABLE;
  105.     },
  106.  
  107.     __docallback: function(rv, callback)
  108.     {
  109.         try {
  110.             if (typeof(callback) == "function") {
  111.                 callback(rv);
  112.             }
  113.         } catch (e) {
  114.             dump("wot_extapi.__docallback: failed with " + e + "\n");
  115.         }
  116.     },
  117.  
  118.     /* Public functions */
  119.     getstatus: function(target, callback)
  120.     {
  121.         try {
  122.             var rv = this.STATUS_UNAVAILABLE;
  123.             var hostname = target;
  124.  
  125.             /* Verify target, parse hostname if necessary */
  126.             if (target) {
  127.                 if (target.indexOf("/") >= 0) {
  128.                     hostname = wot_url.gethostname(target);
  129.                 }
  130.                 if (hostname && !wot_url.isprivate(hostname)) {
  131.                     rv = this.STATUS_AGAIN;
  132.                 }
  133.             }
  134.  
  135.             if (rv == this.STATUS_UNAVAILABLE) {
  136.                 return rv;
  137.             }
  138.  
  139.             /* Make sure the current ratings are updated */
  140.             wot_core.update();
  141.             rv = this.__getcachedstatus(hostname);
  142.  
  143.             if (rv == this.__STATUS_INPROGRESS) {
  144.                 return this.__pollstatus(target, callback);
  145.             }
  146.  
  147.             /* Request missing ratings */
  148.             if (rv == this.STATUS_AGAIN && 
  149.                     !wot_api_query.send(hostname, function() {
  150.                             /* Called when request completes */
  151.                             var s = this.__getcachedstatus(hostname);
  152.                             if (s == this.STATUS_AGAIN) {
  153.                                 s = this.STATUS_UNAVAILABLE;
  154.                             }
  155.                             this.__docallback(s, callback);
  156.                         })) {
  157.                 return this.STATUS_UNAVAILABLE;
  158.             }
  159.         
  160.             this.__docallback(rv, callback);
  161.             return rv;
  162.         } catch (e) {
  163.             dump("wot_extapi.getstatus: failed with " + e + "\n");
  164.         }
  165.  
  166.         return this.STATUS_UNAVAILABLE;
  167.     }
  168. };
  169.